-- BridgeHealer.lua (Minimap bridge) — Minimap button fully optional/hidden by default

local addonName, NS = ...
NS = NS or _G.SleekCombatText or _G[addonName] or {}
_G.SleekCombatText = NS

-- ---------- Saved Vars scaffold ----------
NS.db = NS.db or { general = {} }
NS.db.general.hideMinimap = (NS.db.general.hideMinimap == nil) and true or NS.db.general.hideMinimap
-- ^ default = true (hidden)

NS.Minimap = NS.Minimap or {}

-- ---------- Internal: creation ----------
local function CreateMinimapButton()
  -- Don’t create if explicitly hidden, or already created, or Minimap not available
  if NS.db.general.hideMinimap then return end
  if NS.MinimapButton or not Minimap then return end

  local btn = CreateFrame("Button", "SCT_MinimapButton", Minimap)
  btn:SetSize(32, 32)
  btn:SetNormalTexture(134400)     -- generic icon
  btn:SetHighlightTexture(136477)  -- highlight
  btn:SetPoint("TOPLEFT", Minimap, "TOPLEFT", 10, -10)

  btn:SetScript("OnClick", function(self, button)
    if button == "LeftButton" then
      if InterfaceOptionsFrame_OpenToCategory then
        -- Open your main options category if you have one registered
        InterfaceOptionsFrame_OpenToCategory("流畅战斗文本 – 通用")
        InterfaceOptionsFrame_OpenToCategory("流畅战斗文本 – 通用")
      end
    elseif button == "RightButton" then
      if NS.Healer and NS.Healer.TestShowHeals then NS.Healer.TestShowHeals() end
    end
  end)

  btn:SetScript("OnEnter", function(self)
    GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
    GameTooltip:SetText("流畅战斗文本")
    GameTooltip:AddLine("左键点击: 打开选项", 1, 1, 1)
    GameTooltip:AddLine("右键点击: 治疗测试", 1, 1, 1)
    GameTooltip:Show()
  end)
  btn:SetScript("OnLeave", function() GameTooltip:Hide() end)

  NS.MinimapButton = btn
end

-- ---------- Visibility helpers ----------
function NS.Minimap.UpdateVisibility()
  -- Create only if needed and allowed
  if not NS.db.general.hideMinimap and not NS.MinimapButton then
    CreateMinimapButton()
  end
  if not NS.MinimapButton then return end
  if NS.db.general.hideMinimap then
    NS.MinimapButton:Hide()
  else
    NS.MinimapButton:Show()
  end
end

-- Public toggle you can call from anywhere:
--   /run SleekCombatText.Minimap.SetHidden(true)  -- hides (and prevents creation)
--   /run SleekCombatText.Minimap.SetHidden(false) -- shows (creates if missing)
function NS.Minimap.SetHidden(hidden)
  hidden = not not hidden
  NS.db.general.hideMinimap = hidden
  NS.Minimap.UpdateVisibility()
end

-- ---------- Init ----------
local f = CreateFrame("Frame")
f:RegisterEvent("PLAYER_ENTERING_WORLD")
f:SetScript("OnEvent", function()
  -- Only create the button if not hidden; otherwise do nothing.
  NS.Minimap.UpdateVisibility()
end)
